public without sharing class LinktoContact 
{
public List<WrperLinktoContact> lstWrperLinktoContact{get;set;}
public LinktoContact()
{
lstWrperLinktoContact=new List<WrperLinktoContact>();
string AccountId= ApexPages.currentPage().getParameters().get('AccountId');

List<Contact> lstcontact=[Select Id,Phone,MailingState,MailingCity,Sync_Lock__c,LastName,FirstName,Email,RecordType.Name From Contact where AccountId=:AccountId];
 for(Contact con:lstcontact)
    {
        WrperLinktoContact wper=new WrperLinktoContact(con);
        //wper.contactid=con.Id;
        lstWrperLinktoContact.add(wper);
    }
    //Contact cont=new Contact(Phone='',MailingState='',MailingCity='',LastName='',FirstName='',Email='');
    
    WrperLinktoContact wper=new WrperLinktoContact(new Contact());
    lstWrperLinktoContact.add(wper);
}


public PageReference Link() 
{
    String Linkcon= ApexPages.currentPage().getParameters().get('Linkcon');
    string CaseId= ApexPages.currentPage().getParameters().get('CaseId');
    system.debug('testing1'+CaseId+'tes'+Linkcon);
    Case cas=[Select Id,ContactId from Case where Id=:CaseId];
    cas.ContactId=Linkcon;
     try 
     {
    update cas;
    }
    catch(DmlException ex)
     {
        ApexPages.addMessages(ex);
        return null;
     }
    system.debug('testing'+cas);
    PageReference pageref=new PageReference('/'+CaseId);
    //pageref.setredirect(true);
    return pageref;
}
public PageReference AddContact() 
{
string attcon;
string iogcon;
string conrectype;
string conemail;
string AccountId= ApexPages.currentPage().getParameters().get('AccountId');
string CaseId= ApexPages.currentPage().getParameters().get('CaseId');
List<RecordType> lstRectype = [SELECT Id,Name FROM RecordType WHERE SobjectType='Contact' and name in('ATT Professional','IOG Contact')];
for(RecordType rectype:lstRectype)
{
if(rectype.name=='ATT Professional')
attcon=rectype.Id;
if(rectype.name=='IOG Contact')
iogcon=rectype.Id;
}
Contact con=new Contact();
    for(WrperLinktoContact wper:lstWrperLinktoContact)
    {
        if(wper.cont.Id==null)
        {           
            con.Phone=wper.cont.Phone;
            con.MailingState=wper.cont.MailingState;
            con.MailingCity=wper.cont.MailingCity;
            con.LastName=wper.cont.LastName;
            con.FirstName=wper.cont.FirstName;
            con.Email=wper.cont.Email;
            conemail=con.Email;
            con.RecordTypeId=wper.selRectype;
            conrectype=wper.selRectype;
           
            
        }
    }
     system.debug('LastName'+con.LastName+'FirstName'+con.FirstName+'MailingState'+con.MailingState+'Phone'+con.Phone+'Email'+con.Email);
    
   if (con.LastName==null || con.FirstName==null || con.MailingState==null) 
     {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'First Name, Last Name, Contact Phone or Contact Email, Mailing State required.'));
            return null;            
     } 
      system.debug('LastName'+con.LastName+'FirstName'+con.FirstName+'MailingState'+con.MailingState+'Phone'+con.Phone+'Email'+con.Email);
     if(con.Phone==null && con.Email==null)
    {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'First Name, Last Name, Contact Phone or Contact Email, Mailing State required.'));
            return null;            
     } 
      
     try 
    {
     if(conrectype==iogcon)
        {
            List<Contact> lstcontact=[Select Id,Email,RecordType.Name From Contact where AccountId=:AccountId and Email=:conemail];
            system.debug('testlstcontact'+lstcontact);
            if(lstcontact.size()==0)
            {
                con.AccountId=AccountId;
                insert con; 
                Case cas=[Select Id,ContactId from Case where Id=:CaseId];
                cas.ContactId=con.id;
                update cas;
            }
            else
            {
                 if(lstcontact.size()>1)
                {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'There are multiple contacts have this email address. Please delete unwanted contacts'));
                return null;
                }
                 else if(lstcontact.size()==1)
                {
                string conid=lstcontact[0].Id;
                Case cas=[Select Id,ContactId from Case where Id=:CaseId];
                cas.ContactId=conid;
                update cas;
                }               
               /* if(cas.ContactId==conid)
                {
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'This contact is already there and linked with this case'));
                return null; 
                }*/
                
            }
        }
        else if(conrectype==attcon)
        {
            List<Account> lstaccount=[Select id from Account where name='ATT Professionals'];
            string AttAccountId=lstaccount[0].Id;
            List<Contact> lstcontact=[Select Id,Email,RecordType.Name From Contact where AccountId=:AttAccountId and Email=:conemail];
            if(lstcontact.size()==0)
            {
                con.AccountId=AttAccountId;
                insert con; 
                Case cas=[Select Id,ContactId from Case where Id=:CaseId];
                cas.ContactId=con.id;
                update cas;
                system.debug('casecon'+cas+con);
            }
            else
            {
                if(lstcontact.size()>1)
                {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'There are multiple contacts have this email address. Please delete unwanted contacts'));
                return null;
                }
                else if(lstcontact.size()==1)
                {
                string conid=lstcontact[0].Id;              
                Case cas=[Select Id,ContactId from Case where Id=:CaseId];
                cas.ContactId=conid;
                update cas;
                }
                
                /*if(cas.ContactId==conid)
                {
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'This contact is already there and linked with this case'));
                return null; 
                }
                else
                {
                cas.ContactId=conid;
                update cas;
                }*/
            }
        
        }
   
    }
     catch(DmlException ex)
     {
        ApexPages.addMessages(ex);
        return null;
     }
    PageReference pageref=new PageReference('/'+CaseId);
   // pageref.setredirect(true);
    return pageref;

}
public PageReference Cancel()
{
string CaseId= ApexPages.currentPage().getParameters().get('CaseId');
 PageReference pageref=new PageReference('/'+CaseId);
   // pageref.setredirect(true);
    return pageref;
}

}